<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="close">
    <el-form ref="form" :model="form" label-width="100px" label-position="left" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" @focus="focusFn">
          <el-option v-for="item in easyList" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" resize="none" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="addSubmitFn">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartmentList, addDepartment, getDepartmentDetail, editDepartment } from '@/api/department'
import { getEasyList } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: true
    },
    currentData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      // 1、发送请求，获取所有的最新的部门列表
      const { depts } = await getDepartmentList()
      if (this.form.id) {
        if (value === this.currentData.name) return callback()
        const result = depts.filter(item => item.pid === this.currentData.pid)
        const have = result.some(item => item.name === value)
        have ? callback(new Error(`${value}已经存在了`)) : callback()
      } else {
      // 2、根据currentData拿到所有的子部门
      // console.log(this.currentData)
        const result = depts.filter(item => item.pid === this.currentData.id)
        // console.log(result)
        // 3、判断value是否和子部门的名字重复
        const have = result.some(item => item.name === value)
        have ? callback(new Error(`${value}已经存在了`)) : callback()
      }
    }
    const checkCode = async(rule, value, callback) => {
      if (this.form.id) {
        if (value === this.currentData.code) {
          return callback()
        }
      }
      // 1、发送请求，获取所有的最新的部门列表
      const { depts } = await getDepartmentList()
      // 2、判断value是否和所有部门有重复的code
      const have = depts.some(item => item.code === value)
      have ? callback(new Error(`${value}编码已经存在了`)) : callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { max: 50, message: '部门名称长度1-50位', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { max: 50, message: '部门编码长度1-50位', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { max: 300, message: '部门介绍长度为1-300位', trigger: 'blur' }
        ]
      },
      easyList: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '添加子部门'
    }
  },
  methods: {
    close() {
      this.$emit('changShow')
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    async focusFn() {
      const data = await getEasyList()
      // console.log(data)
      this.easyList = data
    },
    async addSubmitFn() {
      // console.log(111)
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      if (this.form.id) {
        console.log(this.form)
        const rs = await editDepartment(this.form)
        console.log(rs)
      } else {
        await addDepartment({
          ...this.form,
          pid: this.currentData.id
        })
      }
      this.$message.success('操作成功')
      this.$emit('changShow')
      this.$emit('getList')
    },
    async getEditDpmt(id) {
      const data = await getDepartmentDetail(id)
      console.log(data)
      this.form = data
    }
  }
}
</script>

<style>

</style>
